package com.y2.authprj.mvc.controller.auth;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.gdglc.spring.annotations.AuthValidate;
import com.gdglc.spring.biz.auth.ISysroleBiz;
import com.gdglc.spring.biz.auth.StaffInfoBiz;
import com.gdglc.spring.biz.auth.UserroleInfoBiz;
import com.y2.authprj.bean.auth.model.Page;
import com.y2.authprj.bean.auth.model.StaffInfo;
import com.y2.authprj.bean.auth.model.SysroleInfo;
import com.y2.authprj.bean.auth.model.UserroleInfo;
import com.y2.authprj.bean.auth.vo.ResponeVo;
import com.y2.authprj.bean.auth.vo.ShowUserroleVo;
import com.y2.authprj.bean.auth.vo.StaffInfoVo;
import com.y2.authprj.bean.auth.vo.UserroleVo;
import com.y2.authprj.exception.BizException;


@Controller
@RequestMapping("/admin/userrole")
public class UserroleInfoController {
	@Resource(name="StaffInfoBiz")
	private StaffInfoBiz staffBiz;
	
	@Resource(name="UserroleInfoBiz")
	private UserroleInfoBiz userroleBiz;
	
	@Resource(name="ISysroleBiz")
	private ISysroleBiz sysroleBiz;
	
	
	
	@RequestMapping("/showAdd")
	public String showAdd(UserroleVo vo,Model model){
		List<StaffInfo> staffList=new ArrayList<StaffInfo>();
		StaffInfo staffInfo=new StaffInfo();
		Page<StaffInfo> tStaffPage=new Page<StaffInfo>();
		try {
			tStaffPage=staffBiz.showStaff(vo);
		} catch (Exception e) {
			e.printStackTrace();
			model.addAttribute("message",e.getMessage());
		}

		List<SysroleInfo> sysroleList=sysroleBiz.findAllSysrole();
		
		for (StaffInfo Info : tStaffPage.getList()) {
			staffInfo=Info;
			String ageLimit=getAgeLimit(Info.getCreateTime());
			staffInfo.setAgeLimit(ageLimit);
			staffList.add(staffInfo);
		}
		model.addAttribute("staffList", staffList);
		model.addAttribute("toolPage", tStaffPage.getTotalPageCount());
		model.addAttribute("sysroleList", sysroleList);
		return "admin/add";
	}
	
	public String getAgeLimit(Date date){
		DecimalFormat df   = new DecimalFormat("######0.00");   
		Date newDate=new Date();
		//算出来时间夸格多长
		long shiChang = newDate.getTime()-date.getTime();
		//转换成天
		int day = (int)(shiChang/(1000*60*60*24));
		//转换成想要的格式，这里定为1年=365天 1月=30天
		double year =0;
		if(day/365==0){
			if(day/30==0){
				year=day;
				return year+"天";
			}else{
				year=day/30.00;
				return df.format(year)+"月";
			}
			
		}else{
			//输出结果
			year=day/365.00;
			return df.format(year)+"年";
		}
		
		
	}
	
	@RequestMapping("/showUpdate")	
	public String showUpdate(Integer id,UserroleVo vo,Model model){
		List<StaffInfo> staffList=new ArrayList<StaffInfo>();

		Page<StaffInfo> tStaffPage=new Page<StaffInfo>();
		ShowUserroleVo showVo=new ShowUserroleVo();
		UserroleInfo userrole=new UserroleInfo();
		try {
			userrole=userroleBiz.findById(id);
			StaffInfo staffInfo=staffBiz.StaffInfoById(id);
			showVo.setUserroleId(userrole.getUserroleId());
			showVo.setStaffNum(staffInfo.getStaffNum());
		} catch (Exception e) {
			e.printStackTrace();
			model.addAttribute("message",e.getMessage());
		}
		
		try {
			tStaffPage=staffBiz.showStaff(vo);
		} catch (Exception e) {
			e.printStackTrace();
			model.addAttribute("message",e.getMessage());
		}
		
		for (StaffInfo Info : tStaffPage.getList()) {
			StaffInfo staffInfo=new StaffInfo();
			staffInfo=Info;
			String ageLimit=getAgeLimit(Info.getCreateTime());
			staffInfo.setAgeLimit(ageLimit);
			staffList.add(staffInfo);
				
		}
		List<SysroleInfo> sysroleList=sysroleBiz.findAllSysrole();
		for (SysroleInfo sysroleInfo : sysroleList) {
			if(sysroleInfo.getRoleId()==userrole.getRoleId()){
				showVo.setSysroleName(sysroleInfo.getName());
			}
		}
		
		model.addAttribute("staffList", staffList);
		model.addAttribute("toolPage", tStaffPage.getTotalPageCount());
		model.addAttribute("sysroleList", sysroleList);
		
		model.addAttribute("showVo", showVo);
		model.addAttribute("userrole", userrole);
		return "admin/update";
	}
	
	//@AuthValidate("修改角色分配信息")
	@RequestMapping("/update")
	@ResponseBody
	public ResponeVo update(UserroleInfo userrole,RedirectAttributes model){		
		ResponeVo vo = new ResponeVo();
		try {
			userroleBiz.updateUserroleInfo(userrole);
		}catch(BizException e){
			vo.setCode(ResponeVo.ERROR);
			vo.setMessage(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return vo;
		
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public ResponeVo add(Integer roleId,Integer[] staffId){
		ResponeVo vo = new ResponeVo();
		try {
			if(staffId.length>0){
				for (Integer  sId: staffId) {
					UserroleInfo info=new UserroleInfo();
					info.setRoleId(roleId);
					info.setStaffId(sId);
					userroleBiz.addUserroleInfo(info);
				}	
			}else{
				
				vo.setCode(ResponeVo.ERROR);
				vo.setMessage("请指定添加的行!");
			}	
		} catch (BizException e) {
			e.printStackTrace();
			vo.setCode(ResponeVo.ERROR);
			vo.setMessage(e.getMessage());
		}catch (Exception e) {
			e.printStackTrace();
			vo.setCode(ResponeVo.ERROR);
			vo.setMessage("操作失败!");
		}
		return vo;
	}
	
	//@AuthValidate("删除角色分配信息")
	@RequestMapping("/del")
	@ResponseBody
	public ResponeVo delet(int[] delArray){
		ResponeVo vo = new ResponeVo();
		try {
			if(delArray.length>0 ){
				for (int id : delArray) {
					userroleBiz.deleteUserroleInfoById(id);
				}
			}else{
				vo.setCode(ResponeVo.ERROR);
				vo.setMessage("请指定删除的行!");
			}
			
		}catch(BizException e){
			vo.setCode(ResponeVo.ERROR);
			vo.setMessage(e.getMessage());
		}catch (Exception e) {
			e.printStackTrace();
			vo.setCode(ResponeVo.ERROR);
			vo.setMessage("操作失败!");
		}
		return vo;
	}
	
	
	
	
	@RequestMapping("/showUserrole")
	public String showUserrole(UserroleVo vo,Model model){
		Page<ShowUserroleVo> userrolePage=new Page<ShowUserroleVo>();
		
		ShowUserroleVo showVo=new ShowUserroleVo();
		List<ShowUserroleVo> listVo=new ArrayList<ShowUserroleVo>();
		try {
			userrolePage=userroleBiz.findByUserroleVo(vo);			
		} catch (Exception e) {
			e.printStackTrace();
			model.addAttribute("message",e.getMessage());
		}
		for (ShowUserroleVo Info : userrolePage.getList()) {
			showVo=Info;
			String ageLimit=getAgeLimit(Info.getCreateDate());
			showVo.setAgeLimit(ageLimit);
			listVo.add(showVo);
		}
		List<SysroleInfo> sysroleList=sysroleBiz.findAllSysrole();
		
		model.addAttribute("sysroleList", sysroleList);
		model.addAttribute("listVo", listVo);
		model.addAttribute("toolPage", userrolePage.getTotalPageCount());		
		return "admin/admin";
	}
	
			
	
	
			//显示用户添加角色
			@RequestMapping("/showStaffAdd")	
			public String showStaffAdd(int staffId,Model model){	
				if(staffId<=0){
					model.addAttribute("message","用户Id出错，请返回重新操作！");
					return "admin/staffAdd";
				}
				StaffInfo staffInfo=new StaffInfo();
				List<SysroleInfo> sysroleInfoList=new ArrayList<SysroleInfo>();
				List<UserroleInfo> userroleInfoList=new ArrayList<UserroleInfo>();
				try {
					staffInfo=staffBiz.StaffInfoById(staffId);
					sysroleInfoList=sysroleBiz.findAllSysrole();
					userroleInfoList=userroleBiz.findByStaffId(staffId);
				} catch (Exception e) {
					e.printStackTrace();
					model.addAttribute("message",e.getMessage());
				}
						
				model.addAttribute("staffInfo", staffInfo);
				model.addAttribute("sysroleList", sysroleInfoList);
				model.addAttribute("userroleList", userroleInfoList);
				
				return "admin/staffAdd";
			}
		
		
		
			/***
			 * 根据用户添加角色
			 * @param roleId
			 * @param staffId
			 * @return
			 */
			@RequestMapping("/staffAdd")
			@ResponseBody
			public ResponeVo staffAdd(Integer staffId,Integer[] roleId){
				ResponeVo vo = new ResponeVo();
				try {
					if(roleId.length>0){
						userroleBiz.addByStaff(staffId, roleId);	
					}else{
						vo.setCode(ResponeVo.ERROR);
						vo.setMessage("请指定添加的行!");
					}	
				} catch (BizException e) {
					e.printStackTrace();
					vo.setCode(ResponeVo.ERROR);
					vo.setMessage(e.getMessage());
				}catch (Exception e) {
					e.printStackTrace();
					vo.setCode(ResponeVo.ERROR);
					vo.setMessage("操作失败!");
				}
				return vo;
			}
	
}
